package com.cyzy.controller;

import com.cyzy.entity.TeacherAvailableTime;
import com.cyzy.service.TeacherAvailableTimeService;
import com.cyzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/course")
public class TeacherAvailableTimeController {

    @Autowired
    private TeacherAvailableTimeService teacherAvailableTimeService;

    // 1. 分页查询课程列表
    @PostMapping("/list")
    public Result<Map<String, Object>> getList(@RequestBody Map<String, Object> params) {
        String keyword = (String) params.get("keyword");
        String teacherName = (String) params.get("teacherName");
        int pageNum = params.get("pageNum") != null ? (Integer) params.get("pageNum") : 1;
        int pageSize = params.get("pageSize") != null ? (Integer) params.get("pageSize") : 10;

        Map<String, Object> result = teacherAvailableTimeService.getList(keyword, teacherName, pageNum, pageSize);
        return Result.success(result);
    }

    // 2. 根据ID查询课程
    @PostMapping("/getById")
    public Result<TeacherAvailableTime> getById(@RequestBody Map<String, Object> params) {
        Integer id = params.get("id") != null ? (Integer) params.get("id") : null;
        TeacherAvailableTime teacherAvailableTime = teacherAvailableTimeService.getById(id);
        return Result.success(teacherAvailableTime);
    }

    // 3. 新增课程
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody TeacherAvailableTime teacherAvailableTime) {
        boolean success = teacherAvailableTimeService.add(teacherAvailableTime);
        return Result.success(success);
    }

    // 4. 更新课程
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody TeacherAvailableTime teacherAvailableTime) {
        boolean success = teacherAvailableTimeService.update(teacherAvailableTime);
        return Result.success(success);
    }

    // 5. 删除课程
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody Map<String, Object> params) {
        Integer id = params.get("id") != null ? (Integer) params.get("id") : null;
        boolean success = teacherAvailableTimeService.delete(id);
        return Result.success(success);
    }
}
